package com.burst.reptile.modular.api.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.burst.reptile.common.constant.UserConstants;
import com.burst.reptile.common.utils.poi.ExcelUtil;
import com.burst.reptile.framework.web.controller.BaseController;
import com.burst.reptile.framework.web.result.LayuiResult;
import com.burst.reptile.framework.web.result.Result;
import com.burst.reptile.modular.zp.entity.ZpLabel;
import com.burst.reptile.modular.zp.service.IZpLabelService;

import lombok.RequiredArgsConstructor;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wujin
 * @since 2020-06-16
 */
@RestController
@RequestMapping("/zp/label")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ZpLabelController extends BaseController{
	
	private final IZpLabelService labelService;
	
	/**
	 * 分页获取标签列表
	 * @param role
	 * @return
	 */
	@PreAuthorize("@ss.hasPermi('zp:label:list')")
	@GetMapping("/list")
	public LayuiResult<Object> list(ZpLabel label) {
		Page<ZpLabel> page = startPage();
		Page<ZpLabel> list = labelService.pageList(page, label);
		return LayuiResult.success(list.getTotal(), list.getRecords());
	}
	
	/**
	 * 分页获取标签列表
	 * @param role
	 * @return
	 */
	@PreAuthorize("@ss.hasPermi('zp:label:add')")
	@PostMapping("/add")
	public Result<Object> add(@Validated @RequestBody ZpLabel label) {
		if (UserConstants.NOT_UNIQUE.equals(labelService.checkLabelKeyUnique(label))) {
			return Result.error("新增标签'" + label.getLabelName() + "'失败，已存在，不能重复添加");
		}
		return Result.success(labelService.insertLabel(label));
	}
	
	/**
	 * 分页获取标签列表
	 * @param role
	 * @return
	 */
	@PreAuthorize("@ss.hasPermi('zp:label:edit')")
	@PutMapping("/edit")
	public Result<Object> edit(@Validated @RequestBody ZpLabel label) {
		if (UserConstants.NOT_UNIQUE.equals(labelService.checkLabelKeyUnique(label))) {
			return Result.error("标签'" + label.getLabelName() + "'已存在");
		}
		return Result.success(labelService.updateLabel(label));
	}
	
	/**
	 * 批量删除标签集
	 * @param ids 标签ID集
	 * @return
	 */
	@PreAuthorize("@ss.hasPermi('zp:label:remove')")
	@DeleteMapping("/remove/{ids}")
	public Result<Object> remove(@PathVariable Long[] ids) {
		return Result.success(labelService.deleteByIds(ids));
	}
	
	/**
	 * 导出标签
	 * @return
	 */
	@PreAuthorize("@ss.hasPermi('zp:label:export')")
	@GetMapping("/export")
	public Result<Object> export() {
		List<ZpLabel> list = labelService.list(new QueryWrapper<ZpLabel>().orderByDesc("label_heat"));
		ExcelUtil<ZpLabel> util = new ExcelUtil<ZpLabel>(ZpLabel.class);
		return util.exportExcel(list, "标签数据");
	}

}
